﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace kostenbuch
{
    /// <summary>
    /// Interaktionslogik für ChartPage.xaml
    /// </summary>
    public partial class ChartPage : Page
    {
        Calculator calc = new Calculator();
        double[] katPercent, inKatPercent;
        string[] kategorien = new string[kostenbuch.Properties.Settings.Default.Kategorie.Count];
        int rowCount = 0;
        int index = 0;       
        public ChartPage()
        {
            InitializeComponent();
            kostenbuch.Properties.Settings.Default.Kategorie.CopyTo(kategorien, 0);
            katPercent = calc.getKatPercent();
            inKatPercent = calc.getInKatPercent();
            createDiagramsPie(katPercent, canvasPie);
            double e = (calc.getSumOfExpenses() / (calc.getSumOfExpenses() + calc.getSumOfIncome())) * 100;
            double a = (calc.getSumOfIncome() / (calc.getSumOfExpenses() + calc.getSumOfIncome())) * 100;            
            createDiagramsBar(canvasBar,e,a,true);
            rowCount += 12 - rowCount;
            createDiagramsPie(inKatPercent, canvasPie2);
            canvas_proKonto.Children.RemoveRange(0, 2);
            draw_ein_aus_pro_konto(index);
            l2_kontoS.Foreground = Brushes.Blue;
            l2_kontoS.Content = "Kontostand: " + calc.getAccountBalance()[index] + " €";
        }
        /*
        *  Ruft die Methoden zum erstellen der BalkenDiagramme und Labels auf.
        */
        public void createDiagramsBar(Canvas c, double a,double b, bool first)
        {
            DrawChart dChart2 = new DrawChart();
            c.Children.Add(dChart2.drawBar(a, c));
            c.Children.Add(dChart2.drawBar(b, c));
            labels(first,a,b);
        }
        /*
        * Erstellt Labels für die Balkendiagramme
        */
        private void labels(bool first,double a,double b)
        {
            if (first)
            {
                if (a.ToString().Length > 4)
                {
                    String sA = a.ToString().Substring(0, 4);
                    l_out.Content += " " + sA + " %" + " -> " + calc.getSumOfExpenses() + " €";
                }
                else
                {
                    l_out.Content += " " + a + " %" + " -> " + calc.getSumOfExpenses() + " €";
                }
                if (b.ToString().Length > 4)
                {
                    String sB = b.ToString().Substring(0, 4);
                    l_in.Content += " " + sB + " %" + " -> " + calc.getSumOfIncome() + " €";
                }
                else
                {
                    l_in.Content += " " + b + " %" + " -> " + calc.getSumOfIncome() + " €";
                }
                l_in.Foreground = Brushes.Green;
                l_out.Foreground = Brushes.DarkRed;
            }
            else
            {                
                l2_ein.Content = "Einnahmen: " + calc.getEProKonto()[index] + " €";
                l2_aus.Content = "Ausgaben: " + calc.getAProKonto()[index] + " €";
                l2_ein.Foreground = Brushes.Green;
                l2_aus.Foreground = Brushes.DarkRed;                
            }
        }
        //Fenster zum Hinzufuegen neuer Eintraege anzeigen
        private void click_new_form(object sender, RoutedEventArgs e)
        {
            form formular = new form();
            formular.ShowDialog();
        }
        /*
        * Erstellt für alle Kategorien die mehr als 5% TortendiagrammStück und ein Label.
        */
        public void createDiagramsPie(double[] katPerc,Canvas c)
        {          
            DrawChart dChart = new DrawChart();
            int count = 0;           
            double less = 0.0;
            for (int i = 0; i < katPerc.Length; i++)
            {          
                Label l = new Label();
                Label lK = new Label();              
                if (!(katPerc[i] < 5) && !(double.NaN.CompareTo(katPerc[i])==0))
                {
                    Path p = dChart.drawPie(katPerc[i], c);
                    c.Children.Add(p);
                    String lS = katPerc[i].ToString();
                    rowCount++;
                    if(lS.Length> 4)
                    lS = lS.Substring(0, 3); 
                    l.Content = lS + " %";
                    lK.Content = kategorien[i];
                    l.Margin = new Thickness(320, 20 * rowCount + 50, 700, 0);
                    lK.Margin = new Thickness(360, 20 * rowCount + 50, 650, 0);    
                    #region switch-case für Farben                           
                    switch (count)
                    {
                        case 0:
                                {
                                    l.Foreground = Brushes.Red;
                                    count++;
                                    break;
                                }
                        case 1:
                                {
                                    l.Foreground = Brushes.Blue;
                                    count++;
                                    break;
                                }
                        case 2:
                                {
                                    l.Foreground = Brushes.Orange;
                                    count++;
                                    break;
                                }
                        case 3:
                                {
                                    l.Foreground = Brushes.Indigo;
                                    count++;
                                    break;
                                }
                        case 4:
                                {
                                    l.Foreground = Brushes.Fuchsia;
                                    count++;
                                    break;
                                }
                        case 5:
                                {
                                    l.Foreground = Brushes.DeepSkyBlue;
                                    count++;
                                    break;
                                }
                        case 6:
                                {
                                    l.Foreground = Brushes.DeepPink;
                                    count = 1;
                                    break;
                                }
                        default:
                                {
                                    l.Foreground = Brushes.Green;
                                    break;
                                }
                    }
                    #endregion
                    grid1.Children.Add(l);
                    grid1.Children.Add(lK);                             
                }else
                {
                    less += katPerc[i];
                } 
            }        
            c.Children.Add(dChart.drawPieMisc(less, c));
            if (less > 1)
            { 
                Label lLess = new Label();
                Label lLessName = new Label();            
                string less_subString = less.ToString();
                if (less_subString.Length > 4)
                {
                    less_subString = less_subString.Substring(0, 3);
                }
                rowCount++;                
                lLess.Foreground = Brushes.Gray;
                lLess.Content = less_subString + " %";
                lLessName.Content = "Sonstiges";
                lLess.Margin = new Thickness(320, 20 * rowCount + 50, 700, 0);
                lLessName.Margin = new Thickness(360, 20 * rowCount+ 50, 650, 0);                              
                grid1.Children.Add(lLess);
                grid1.Children.Add(lLessName);                    
           }
        }
        /*
        * Button Event
        * Leitet den User zur TabelChart.
        */
        private void chartButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService nav = NavigationService.GetNavigationService(this);
            Uri uri = new Uri("TablePage.xaml", UriKind.RelativeOrAbsolute);
            nav.Navigate(uri);
        }
        /*
        * ComboBox Event 
        * Pickt Das ausgewählte Konto als Item-Index
        * Und übergibt es an die draw_ein_aus_pro_Konto Methode
        */
        private void cb_konto_DropDownClosed(object sender, EventArgs e)
        {
            canvas_proKonto.Children.RemoveRange(0,2);
            index = cb_konto.SelectedIndex;
            draw_ein_aus_pro_konto(index);
            l2_kontoS.Content = "Kontostand: " + calc.getAccountBalance()[index] + " €";
        }
        /*
        * Sorgt für die Darstellung des Übergebenen Kontos als Balkendiagramm getrennt in Ein-und Ausgaben
        */
        private void draw_ein_aus_pro_konto(int index)
        { 
            double[] eProK = calc.getEProKonto();
            double[] aProK = calc.getAProKonto();            
            double ein = (eProK[index] / (eProK[index] + aProK[index])) * 100;
            double aus = (aProK[index] / (eProK[index] + aProK[index])) * 100;           
            createDiagramsBar(canvas_proKonto, aus, ein,false);
        }
        private void editKategorie_Click(object sender, RoutedEventArgs e)
        {
            KategorieHinzufügen formular = new KategorieHinzufügen();
            formular.ShowDialog();
        }
        private void editKonto_Click(object sender, RoutedEventArgs e)
        {
            KontoHinzufuegen formular = new KontoHinzufuegen();
            formular.ShowDialog();
        }
        private void tableButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService nav = NavigationService.GetNavigationService(this);
            Uri uri = new Uri("TablePage.xaml", UriKind.RelativeOrAbsolute);
            nav.Navigate(uri);
        }       
    }    
}
